<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>文件管理</title>
    $css()
    <!--<link rel="Stylesheet" href="admin?res=c3R5bGU=&amp;.css" />-->
    <style type="text/css">

        .center {
            text-align: center;
        }

        .hidden {
            display: none;
        }
        th{ font-weight: bold;}

        .ui-table tr div.dir-name {
            color: #006680;
            font-weight: bold;
            padding-left: 3em;
            background: url(public/mui/css/latest/folder.png) 1em center no-repeat;
        }

        .th-split {
            display: none;
        }

        td {
            padding: 3px 5px;
        }

        #control {
            padding: 15px 1% 0 1%;
        }
        .control a{ margin: 0 5px;}

        .data-table thead td {
            background: #EEE;
            text-align: center;
            border-right: solid 1px #ddd;
        }

        .data-table tbody td {
            background: #FFF;
            padding: 3px 10px;
        }

	.data-table tbody td.file-name {padding-left:3em;}
        .data-table tbody input {
            line-height: 1.4em;
            width: 80%;
            margin: 1px auto;
            color: #666;
        }

            .data-table tbody input:focus {
                margin: 1px auto;
            }

        .data-panel .panel {
            overflow-x: hidden;
            border-bottom: solid 1px #ddd;
        }

    </style>
</head>
<body>
 <div id="control">
     <span class="ui-button w100 middle-button" onclick="showCreateDialog(false)">
         <span class="button-inner">
             <span class="button-txt">创建文件</span>
             <a href="javascript:;"></a>
         </span>
     </span>
     &nbsp;
     <span class="ui-button w100 middle-button" onclick="showCreateDialog(true)">
         <span class="button-inner">
             <span class="button-txt">新建文件夹</span>
             <a href="javascript:;"></a>
         </span>
     </span>
     &nbsp;
     <span class="ui-button w100 middle-button" id="btn_upload">
         <span class="button-inner">
             <span class="button-txt">上传文件</span>
             <a href="javascript:;"></a>
         </span>
     </span>

 </div>

    <div class="ui-table data-panel" id="locale-data">
        <table width="100%" class="data-table" cellspacing="0">
            <thead id="thead">
                <tr class="ui-datagrid-header">
                    <th class="fix-col" style="min-width: 100px;">文件(夹)名称</th>
                    <th class="fix-col" width="">大小</th>
                    <th class="fix-col" width="">类型</th>
                    <th class="fix-col" width="">创建时间</th>
                    <th class="fix-col" width="">修改时间</th>
                    <th class="fix-col" width="">属性</th>
                    <th class="fix-col">操作</th>
                </tr>
            </thead>
        </table>

        <div class="panel" id="data-list">
            <table width="100%" class="data-table" cellspacing="0" id="filesexp">
                <tbody></tbody>
            </table>
        </div>
    </div>



<script type="text/template" class="hidden" id="tpl_create_file">
        <div style="padding:2em">
            <b>名称：</b>&nbsp;&nbsp;
            <input type="text" class="ui-box" name="filename" id="tb_fileName" />
            <span class="ui-button w100 middle-button" onclick="window.ctobj.newFile('tb_fileName',{isDir})">
                <span class="button-inner">
                    <span class="button-txt">创建</span>
                    <a href="javascript:;"></a>
                </span>
            </span>
        </div>
    </script>

$js()
<script type="text/javascript">
    /*var cms = new js();*/
    var fm_handler = '?module=file&action=filemanager';
    var curdir = (location.hash || '#/').substring(1);
    var tb = jr.$('filesexp');
    var createTpl = jr.$('tpl_create_file').innerHTML;

    
    var tds = jr.$('thead').getElementsByTagName('TD');
    function fixTitRow() {
        var srcTds = tb.getElementsByTagName('TR')[0].getElementsByTagName('TD');
        for (var i = 0; i < tds.length; i++) {
            var width = Math.floor(srcTds[i].offsetWidth);
            if (tds.length - 1 == i) {
                tds[i].style.width = ( width+ getScrollWidth()) + 'px';
            } else {
                tds[i].style.width = width+ 'px';
                srcTds[i].style.width =width+ 'px';
            }
        }
        var pl = jr.$('data-list');
        var y = document.documentElement.clientHeight;
        if (pl.scrollHeight > y - 85) {
            pl.style.height = (y - 85) + 'px';
        } else {
            pl.style.height = 'inherit';
        }
    }

    function getScrollWidth() {
        var noScroll, scroll, oDiv = document.createElement("DIV");
        oDiv.style.cssText = "position:absolute; top:-1000px; width:100px; height:100px; overflow:hidden;";
        noScroll = document.body.appendChild(oDiv).clientWidth;
        oDiv.style.overflowY = "scroll";
        scroll = oDiv.clientWidth;
        document.body.removeChild(oDiv);
        return noScroll - scroll;
    }


    function showCreateDialog(isDir) {
        var dg = jr.dialog.create2('新建文件' + (isDir ? '夹' : ''),true,false);
        var htm = jr.template(createTpl, { isDir: (isDir ? 'true' : 'false') });
        dg.write(htm);
    }

    //上传图标
    var up = initUploadCom();

    function initUploadCom() {
        up = jr.upload({
            id: 'btn_upload',
            url: fm_handler + '&act=upload&dir='+curdir
        }, function (result, data) {
            if (data.url) {
                load(curdir);
            } else {
                alert('上传失败：' + data.error);location.reload();
            }
            initUploadCom();
        });
        return up;
    }
    function resetUploadFormAction() {
        var form =  jr.$(up.id).getElementsByTagName("FORM")[0];
        if (form != undefined) {
            form.setAttribute('action', fm_handler + '&amp;act=upload&amp;dir='+curdir);
        }
    }

    var ctobj = {
        tip: function (msg, error) {
            showMsg2(msg);
        },

        resetTable: function () {

            tb.innerHTML = '';  //清空

            var parentDir = '/';
            if (location.hash) {
                parentDir = location.hash.replace(/[^\/]+\/\B/, '').replace('#', '');
            }

            var tr = tb.insertRow(-1);  //插入倒数第一行
            var cell = tr.insertCell(-1);
            for (var i = 0; i < 6; i++) {
                var c = tr.insertCell(-1);
                c.innserHTML = '&nbsp;';
                c.setAttribute('style', 'background:#FFE');
            }
           // cell.setAttribute('colspan', '8');
            cell.setAttribute('style', 'background:#FFE');
            cell.innerHTML = "<a style=\"color:#0066cc\" href=\"#" + escape(parentDir) +"\">&nbsp;&nbsp;[⇮上一级]</a>";
        },
        getFileLen: function (_len) {
            var len = _len;
            if (len > 1024 * 1024) {
                len = (len / 1024 / 1024);
                if (/\.\d\d/.test(len.toString())) {
                    len = len.toFixed(2);
                }
                len = len + 'M';
            } else if (len > 1024) {
                len = (len / 1024);
                if (/\.\d\d/.test(len.toString())) {
                    len = len.toFixed(2);
                }
                len = len + 'K';
            } else {
                len = len + 'b';
            }
            return len;
        },
        getFileType: function (name) {
            switch (name) {
            case 'Web.config': return 'ASP.NET网站配置文件';
            case 'Global.asax': return '全局应用程序文件';
            case 'web.conf': return 'CMS配置文件';
            case 'plugin.config':return '插件配置文件';
            }
            var ext = name.replace(/[\s\S]+\.(.+)\b/, '$1');
            switch (ext.toLowerCase()) {
            case "conf":return '设置文件';
            case "jpeg":
            case 'gif':
            case 'png':
            case 'bmp':
            case 'jpg':
                return ext + '图像格式';

            case 'dll': return '二进制代码文件';
            case 'html': return 'HTML文件';
            case 'part.html': return '部分视图文件';
            }
            return ext.toUpperCase() + '文件';
        },
        insertCell: function (row, html, className, col) {
            var td = row.insertCell(-1);
            td.innerHTML = html;
            if (className) {
                td.className = className;
            }
            if (col) {
                td.setAttribute('colspan', col);
            }
            return td;
        },
        appendDirRow: function (o) {
            var row = tb.insertRow(-1);
            this.insertCell(row, '<input type="hidden" file="' + o.name + '"/>', 'hidden');
            this.insertCell(row, '<div class="dir-name"><a href="#' + curdir + encodeURIComponent(o.name) + '/">' + o.name + '</a></a>', 'dir-name');
            this.insertCell(row, (o.dirnum == 0 ? '' : '目录:' + o.dirnum + '&nbsp;') +
            (o.filenum == 0 ? '' : '文件:' + o.filenum), null, 5);

            this.insertCell(row, (o.system == 1 ? '' :
                    '<a href="javascript:;" onclick="ctobj.rename(this,\'dir\')">重命名</a>' +
                    '|<a href="javascript:;" onclick="ctobj.del(this,\'dir\')">删除</a>'
            ), 'center');
        },
        appendFileRow: function (o) {
            var row = tb.insertRow(-1);
            this.insertCell(row, '<input type="checkbox" file="' + o.name + '"/>', 'hidden');
            this.insertCell(row, o.name, 'file-name');
            this.insertCell(row, this.getFileLen(o.len));
            this.insertCell(row, this.getFileType(o.name));
            this.insertCell(row, o.date, 'center');
            this.insertCell(row, o.mdate, 'center');
            this.insertCell(row, o.readonly == 1 ? '读' : '读 / 写', 'center');
            this.insertCell(row, '<a href="javascript:;" onclick="ctobj.down(this)">下载</a>' +
            (o.system == 1 || o.readonly==1 ? '' :
                    '<a href="javascript:;" onclick="ctobj.edit(this)">编辑</a>' +
                    '<a href="javascript:;" onclick="ctobj.rename(this,\'file\')">重命名</a>' +
                    '<a href="javascript:;" onclick="ctobj.del(this,\'file\')">删除</a>'
            ), 'center control');
        },
        del: function (t, type) {
            var _this = this;
            var tr = t.parentNode.parentNode;
            var trIndex = 0;

            for (var i = 0; i < tb.rows.length; i++) {
                if (tb.rows[i] == tr) {
                    trIndex = i;
                }
            }

            var file = tr.getElementsByTagName('TD')[0].childNodes[0].getAttribute('file');

            $b.dialog.confirm("删除后无法恢复,确定要删除 \"" + file + "\" 吗?", function (r) {
                if (!r) return;
                var isDir = type == 'dir' ? 1 : 0;
                jr.xhr.post(fm_handler, 'act=del&dir='
                    + encodeURIComponent(curdir) + '&is_dir=' + isDir + '&file='
                    + encodeURIComponent(file), function (result) {

                        if (result == "1") {
                            //load(curdir); /*重新加载*/
                            tb.deleteRow(trIndex);
                        } else {
                            _this.tip('系统文件/目录无法删除!', true);
                        }

                    }, function () {
                        _this.tip('删除失败' + (isDir == 1 ? ',目录不为空或' : '') + '无权限删除!', true);
                    });
            });
        },
        rename: function (t, type) {
            var _this = this;
            var tr = t.parentNode.parentNode;
            var tds = tr.getElementsByTagName('TD');
            var file = tds[0].childNodes[0].getAttribute('file');
            var isDir = type == 'dir' ? 1 : 0;
            if (tds[1].getElementsByTagName('INPUT').length == 0) {
                var iput = document.createElement('INPUT');
                iput.value = file;
                tds[1].innerHTML = '';
                tds[1].appendChild(iput);
                t.innerHTML = '保存';
            } else {
                var newfile = tds[1].childNodes[0].value;
                if (file == newfile) {
                    var a = document.createElement("A");
                    a.href = "#" + curdir + encodeURIComponent(file) + "/";
                    a.innerHTML = file;
                    tds[1].innerHTML = '';
                    tds[1].appendChild(a);
                } else {
                    _this.tip('执行更改...');
                    jr.xhr.post(fm_handler, 'act=rename&is_dir=' + isDir + '&dir='
                        + encodeURIComponent(curdir) + '&file=' + encodeURIComponent(file)
                        + '&newfile=' + encodeURIComponent(newfile),
                        function (result) {
                            /*_this.tip('');*/
                            load(curdir);
                        }, function (x) {
                            _this.tip('操作失败!');
                        }
                    );
                }
                t.innerHTML = '重命名';
            }
        },
        edit: function (t) {
            var tr = t.parentNode.parentNode.getElementsByTagName('TD')[0];
            var file = tr.childNodes[0].getAttribute('file');
            var ext = file.replace(/[\s\S]+\.(.+)\b/, '$1');
            var editurl = null;
            switch (ext.toLowerCase()) {
            case "css":
            case "js":
            case "txt":
            case "conf":
            case 'config':
            case 'log':
            case "aspx":
            case "xml":
            case "cs":
                editurl = '?module=file&action=editFile&path=';
                break;
            case "html":
            case "phtml":
                editurl = '?module=template&action=editFile&path=';
                break;
            }
            if (editurl != null) {
                // window.open(editurl + curdir + file);
                parent.FwTab.show(file, editurl + curdir + file);
            } else {
                this.tip('不支持编辑的文件类型!', true);
            }
        },
        newFile: function (id,isDir) {
            var _this = this;
            var fileName = $jr.$fn("#"+id).val();
            if (fileName.length > 0) {
                jr.xhr.post(fm_handler, 'xhr=1&act=create&dir=' + encodeURIComponent(curdir) + '&is_dir='
                    + (isDir ? "true" : "false") + '&file=' + encodeURIComponent(fileName),
                    function(json) {
                        if (json.error) {
                            _this.tip(json.error, true);
                        } else {
                            load(curdir);
                        }
                        var d =  jr.dialog.getDialog();
                        if (d)d.close();

                    }, function(x) {
                    });
            } else {
                alert('请输入名称');
            }
        },
        down: function (t) {
            var tr = t.parentNode.parentNode.getElementsByTagName('TD')[0];
            var file = tr.childNodes[0].getAttribute('file');
            window.open('//' + document.domain + curdir + file);
        }
    };

    function load(dir) {
        // ctobj.tip('加载中...', 0);
        ctobj.resetTable();
        jr.xhr.post(fm_handler, 'act=list&dir=' + dir, function (json) {
            if (json.error) {
                ctobj.tip(json.error, true);
            } else {
                //文件夹
                for (var i = 0; i < json.dirs.length; i++) {
                    ctobj.appendDirRow(json.dirs[i]);
                }

                //文件
                for (var i = 0; i < json.files.length; i++) {
                    ctobj.appendFileRow(json.files[i]);
                }


                //重新设置风格
                jr.table.dynamic(document.getElementsByTagName("TABLE")[0],false,function (row) {
                });

                if (json.dirs.length + json.files.length > 0) {
                    fixTitRow();
                }
            }
        }, function (x) { });

    }

    var _hash = curdir;
    setInterval(function () {
        _hash = (location.hash || '#/').substring(1);
        if (curdir !== _hash) {
            curdir = _hash;
            resetUploadFormAction();
            load(curdir);
        }
    }, 100);

    load(curdir);
</script>



</body>
</html>
